#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>

// PAT A 1026 Table Tennis
// This solution fails on 1 case.

struct Time {
    int hr, min, sec;

    Time() {}

    Time(int hr, int min, int sec) : hr(hr), min(min), sec(sec) {}

    Time operator+(int dMin) const {
        int newMin = min + dMin;
        int newHr = hr + newMin / 60;
        newMin %= 60;
        return Time(newHr, newMin, sec);
    }

    int operator-(const Time &other) const {
        int dHr = hr - other.hr;
        int dMin = min - other.min;
        int dSec = sec - other.sec;
        int total = dHr * 3600 + dMin * 60 + dSec;
        return (total + 30) / 60;
    }

    bool operator<(const Time &other) const {
        if (hr != other.hr) return hr < other.hr;
        if (min != other.min) return min < other.min;
        return sec < other.sec;
    }

    bool operator>(const Time &other) const {
        if (hr != other.hr) return hr > other.hr;
        if (min != other.min) return min > other.min;
        return sec > other.sec;
    }

    bool operator==(const Time &other) const {
        return hr == other.hr && min == other.min && sec == other.sec;
    }

    bool operator!=(const Time &other) const {
        return hr != other.hr || min != other.min || sec != other.sec;
    }

    bool operator>=(const Time &other) const {
        return (*this) == other || (*this) > other;
    }

    void print() const { printf("%02d:%02d:%02d ", hr, min, sec); }
};

struct Player {
    Time arrival;
    int play;  // in minutes
    bool vip;

    Player() {}

    Player(const Time &arrival, int play, int vip)
        : arrival(arrival), play(play), vip(vip) {}
};

struct Table {
    bool vip;
    int served;

    Table() : vip(false), served(0) {}
};

enum EventType { ARRIVAL, LEAVE };

struct Event {
    EventType type;
    Time time;
    int playerIdx;
    int tableIdx;

    Event(EventType type, const Time &time, int playerIdx, int tableIdx = -1)
        : type(type), time(time), playerIdx(playerIdx), tableIdx(tableIdx) {}

    bool operator<(const Event &other) const { 
        if (time != other.time)
            return time > other.time;
        return type > other.type;
    }
};

static const Time OPEN(8, 0, 0), CLOSE(21, 0, 0);

void print(const Time &arrival, const Time &served) {
    int dur = served - arrival;
    arrival.print();
    served.print();
    printf("%d\n", dur);
}

std::vector<Player> players;
std::vector<Table> tables;
std::priority_queue<Event> events;
std::priority_queue<int, std::vector<int>, std::greater<int> > normalTables,
    vipTables;
std::queue<int> normalQueue, vipQueue;

void serve(const Time &time, int playerIdx, int tableIdx) {
    Player &player = players[playerIdx];
    Table &table = tables[tableIdx];
    table.served++;
    print(player.arrival, time);
    Time leaveTime = time + player.play;
    events.push(Event(LEAVE, leaveTime, playerIdx, tableIdx));
}

int main() {
    // Read players
    int nPlayers;
    scanf("%d", &nPlayers);
    players.reserve(nPlayers);
    for (int i = 0; i < nPlayers; i++) {
        int hr, min, sec, playing, vip;
        scanf("%02d:%02d:%02d %d %d", &hr, &min, &sec, &playing, &vip);
        Time time(hr, min, sec);
        players.push_back(Player(time, playing, vip));
        events.push(Event(ARRIVAL, time, i));
    }

    // Read tables
    // tables \in 1..N, index \in 0..N-1
    int nTables, nVip;
    scanf("%d %d", &nTables, &nVip);
    tables.reserve(nTables);
    for (int i = 0; i < nTables; i++) tables.push_back(Table());
    for (int i = 0; i < nVip; i++) {
        int num;
        scanf("%d", &num);
        int tableIdx = num - 1;
        tables[tableIdx].vip = true;
        vipTables.push(tableIdx);
    }
    for (int i = 0; i < nTables; i++)
        if (!tables[i].vip) normalTables.push(i);

    // Begin simulation
    while (!events.empty()) {
        // Pop an event and check its time
        Event event = events.top();
        events.pop();
        if (event.time >= CLOSE) continue;

        // Process according to event time
        Player &player = players[event.playerIdx];
        if (event.type == ARRIVAL) {
            if (player.vip) { // VIP player chooses VIP table first
                if (!vipTables.empty()) {
                    int tableIdx = vipTables.top();
                    vipTables.pop();
                    serve(event.time, event.playerIdx, tableIdx);
                } else if (!normalTables.empty()) {
                    int tableIdx = normalTables.top();
                    normalTables.pop();
                    serve(event.time, event.playerIdx, tableIdx);
                } else {
                    vipQueue.push(event.playerIdx);
                }
            } else {
                // Normal player chooses smallest table
                int tableIdx = INT32_MAX;
                if (!vipTables.empty())
                    tableIdx = std::min(vipTables.top(), tableIdx);
                if (!normalTables.empty())
                    tableIdx = std::min(normalTables.top(), tableIdx);

                // No table available
                if (tableIdx == INT32_MAX) {
                    if (player.vip)
                        vipQueue.push(event.playerIdx);
                    else
                        normalQueue.push(event.playerIdx);
                    continue;
                }

                // Serve the player
                Table &table = tables[tableIdx];
                if (table.vip)
                    vipTables.pop();
                else
                    normalTables.pop();
                serve(event.time, event.playerIdx, tableIdx);
            }

        } else {  // LEAVE
            // Player leaves, choose next player according to table level
            Table &table = tables[event.tableIdx];
            if (table.vip) {  // VIP tables accept VIP players first
                if (!vipQueue.empty()) {
                    int playerIdx = vipQueue.front();
                    vipQueue.pop();
                    serve(event.time, playerIdx, event.tableIdx);
                } else if (!normalQueue.empty()) {  // the normal players
                    int playerIdx = normalQueue.front();
                    normalQueue.pop();
                    serve(event.time, playerIdx, event.tableIdx);
                } else {
                    vipTables.push(event.tableIdx);
                }
            } else {  // normal tables accept earliest player
                // Find earliest player
                int earliestPlayerIdx = -1;
                Time earliestTime = CLOSE;
                if (!vipQueue.empty()) {
                    int playerIdx = vipQueue.front();
                    Time &arrival = players[playerIdx].arrival;
                    if (arrival < earliestTime) {
                        earliestPlayerIdx = playerIdx;
                        earliestTime = arrival;
                    }
                }
                if (!normalQueue.empty()) {
                    int playerIdx = normalQueue.front();
                    Time &arrival = players[playerIdx].arrival;
                    if (arrival < earliestTime) {
                        earliestPlayerIdx = playerIdx;
                        earliestTime = arrival;
                    }
                }

                // No players found, put normal table back
                if (earliestPlayerIdx == -1) {
                    normalTables.push(event.tableIdx);
                    continue;
                }

                // Serve the player
                Player &player = players[earliestPlayerIdx];
                if (player.vip)
                    vipQueue.pop();
                else
                    normalQueue.pop();
                serve(event.time, earliestPlayerIdx, event.tableIdx);
            }
        }
    }

    // Print players served
    for (int i = 0; i < nTables; i++) {
        if (i != 0)
            printf(" ");
        printf("%d", tables[i].served);
    }
    printf("\n");

    return 0;
}